home *** CD-ROM | disk | FTP | other *** search
- /* Mac file system parameters */
- #define MAXPATH 256 /* Max path name length+1 */
- #define SEP ':' /* Separator in path names */
- #define ROOTID 2 /* DirID of a volume's root directory */
- #define NULL 0
-
- #include <string.h>
-
- /* Macro to find out whether we can do HFS-only calls: */
- #define FSFCBLen (* (short *) 0x3f6)
- #define hfsrunning() (FSFCBLen > 0)
-
- static char *
- getdirname(short dir) /* dir = WDrefNum */
- {
- union {
- HFileInfo f;
- DirInfo d;
- WDPBRec w;
- VolumeParam v;
- } pb;
- static char cwd[2*MAXPATH];
- char namebuf[MAXPATH];
- short err;
- long dirid= 0;
- char *next= cwd + sizeof cwd - 1;
- int len;
-
- if (!hfsrunning())
- return "";
-
- for (;;) {
- pb.d.ioNamePtr= (StringPtr)namebuf;
- pb.d.ioVRefNum= dir;
- pb.d.ioFDirIndex= -1;
- pb.d.ioDrDirID= dirid;
- err= PBGetCatInfo((CInfoPBPtr)&pb.d, FALSE);
- if (err != noErr) {
- /* dprintf("PBCatInfo err %d", err); */
- return NULL;
- }
- *--next= SEP;
- len= namebuf[0];
- /* There is no overflow check on cwd here! */
- strncpy(next -= len, namebuf+1, len);
- if (pb.d.ioDrDirID == ROOTID)
- break;
- dirid= pb.d.ioDrParID;
- }
- return next;
- }
-
-
- static void
- fullpath(char *buf, short wdrefnum, char *file)
- {
- strcpy(buf, getdirname(wdrefnum));
- strcat(buf, file);
- }
-
-
-
- /*
-
- Subject: Re: Full path name of a file
- Date: 6 May 88 23:34:14 GMT
-
- >Given the information in a SFReply, how do I construct the full path name
- >of a file (as a string), i e "disk:dir1:dir2:...:filename" ?
-
- Be certain that you understand the difference between "working directory
- reference numbers", which SFReply returns, and directory ID numbers, which
- you will use to find the path name. (I only had to read the file manager
- chapter in Inside Mac Volume IV about ten times before understanding this!).
-
- Basically, you want to know the "real" volume (NOT the working directory
- reference number) and the "real" directory ID, which are two different things,
- neither of which being the working directory reference number. While you can
- take the "working directory reference number" from the SFReply structure and
- convert it into both volume and directory ID numbers, it is easier to take
- advantage of two global variables, CurDirStore and SFSaveDisk (see insert
- on page 72 of Inside Macintosh Vol IV). After any of the SF routines, these
- global variables are set as follows, REGARDLESS OF WHETHER THE USER CHOSE
- A FILE OR CANCELLED:
-
- CurDirStore = directoryID (long int)
- SFSaveDisk = 0 - volume (int)
-
- Thus, if the user changed directories but cancelled the operation, the above
- variables are set the way the user left them (to the new directory). It is
- convenient to keep track of these separately. The following code from MacJove
- shows a simple way to recover the path using these. In this case, the
- current directory is stored in cur_dir and current volume in cur_vol, which
- are set elsewhere (cur_dir - CurDirStore; cur_vol = 0 - SFSaveDisk.
-
- */
-
- static char *getwd(void)
- {
- DirInfo d;
- static char ret[255];
- char nm[50], tmp[255];
-
- ret[0] = '\0';
- d.ioDrDirID = CurDirStore;
- for(;;) {
- d.ioCompletion = 0;
- d.ioNamePtr = (StringPtr) nm;
- d.ioVRefNum = - SFSaveDisk;
- d.ioFDirIndex = -1;
-
- PBGetCatInfo((CInfoPBPtr)&d,0);
- if(d.ioResult != noErr) return(0);
- PtoCstr((char *) nm);
- strcpy(tmp,ret);
- strcpy(ret,":");
- strcat(ret,nm);
- strcat(ret,tmp);
- if(d.ioDrDirID == 2) break; /* home directory */
- d.ioDrDirID = d.ioDrParID;
- }
- return(ret);
- }
-
- void main(void)
- {
- char *foo = getwd();
- }
-